package com.ruoyi.crm.service.impl;

import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.mapper.AssignRecordMapper;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.mapper.ClueMapper;
import com.ruoyi.crm.service.IBusinessService;
import com.ruoyi.crm.service.IClueService;
import com.ruoyi.crm.util.MybatisBatchUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component("crmTask")
@Slf4j
public class CrmTask {
    @Autowired
    private IClueService clueService;
    @Autowired
    private IBusinessService businessService;

    @Autowired
    private MybatisBatchUtils mybatisBatchUtils;

    /**
     * 回收线索
     * 1、已分配或跟进中，下次跟进时间为空的或下次跟进时间超过当前时间的线索
     * 2、修改上述记录状态为已回收
     * 3、修改线索归属人为空
     */
    @Transactional
    public void recoverClue() {
        List<Clue> list = clueService.selectTimeoutClueList();
        log.info("找到{}条待回收线索", list.size());
        if(list.size() > 0) {
            list.forEach(item -> {
                item.setStatus(Clue.Status.Recycled.getValue());
                item.setUpdateTime(new Date());
                item.setUpdateBy("admin");
            });
            int count = mybatisBatchUtils.batchUpdateOrInsert(list, ClueMapper.class, (item, mapper) -> mapper.updateClue(item));
            log.info("成功更新{}条线索状态为‘已回收’", count);
            // 修改线索归属人
            List<Long> idList = list.stream().map(Clue::getId).collect(Collectors.toList());
            count = mybatisBatchUtils.batchUpdateOrInsert(idList, AssignRecordMapper.class, (id, mapper) ->
                    mapper.updateLatestStatus(AssignRecord.Type.CLUE.getValue(), id, AssignRecord.LatestStatus.STALE.getValue()));
            log.info("成功更新{}条分配记录状态为‘非最新’", count);
        }
    }

    /**
     * 回收商机
     * 1、已分配或跟进中，下次跟进时间为空的或下次跟进时间超过当前时间的商机
     * 2、修改上述记录状态为已回收
     * 3、修改商机归属人为空
     */
    @Transactional
    public void recoverBusiness() {
        List<Business> list = businessService.selectTimeoutClueList();
        log.info("找到{}条待回收商机", list.size());
        if(list.size() > 0) {
            list.forEach(item -> {
                item.setStatus(Clue.Status.Recycled.getValue());
                item.setUpdateTime(new Date());
                item.setUpdateBy("admin");
            });
            int count = mybatisBatchUtils.batchUpdateOrInsert(list, BusinessMapper.class, (item, mapper) -> mapper.updateBusiness(item));
            log.info("成功更新{}条线索商机为‘已回收’", count);
            // 修改线索归属人
            List<Long> idList = list.stream().map(Business::getId).collect(Collectors.toList());
            count = mybatisBatchUtils.batchUpdateOrInsert(idList, AssignRecordMapper.class, (id, mapper) ->
                    mapper.updateLatestStatus(AssignRecord.Type.BUSINESS.getValue(), id, AssignRecord.LatestStatus.STALE.getValue()));
            log.info("成功更新{}条分配记录状态为‘非最新’", count);
        }


    }

}
